package com.extweb4j.core.model;

import com.extweb4j.core.enums.RowStatus;
import com.extweb4j.core.kit.ExtKit;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻
 * @author Administrator
 *
 */
public class ExtNews extends CoreModel<ExtNews>{

	
	private static final long serialVersionUID = 1L;

	public static ExtNews dao = new ExtNews();

	public boolean isExist(String str) {
		// TODO Auto-generated method stub
		return super.findBy("Id = ?", str) == null;
	}

	public Page<ExtNews> pageNewsBy(int page, int limit, String keywords) {
		// TODO Auto-generated method stub
		
		String select  =  "select Id as id,ChannelId channelid,source,summary,Title as title,IsChecked as state,CreateTime as create_time,Creater ";
		
		StringBuffer form = new StringBuffer(" FROM Cm_Article c   ");
		
		List<Object> paras = new ArrayList<Object>();
		if(StringUtils.isNotBlank(keywords)){
			keywords = ExtKit.sqlFilterValidate(keywords);
			form.append(" AND (title LIKE '%"+keywords+"%' OR Source LIKE '%"+keywords+"%' ");
			form.append(" OR Summary LIKE '%"+keywords+"%' )");
		}
		form.append(" ORDER BY CreateTime DESC");
		return super.paginate(page, limit, select, form.toString(), paras.toArray());
	}
	/**
	 * 根据id 获取新闻实体类
	 * @Param id
	 * @return
	 */
	public ExtNews findNewsById(String id) {
		// TODO Auto-generated method stub
		return ExtNews.dao.findFirst("SELECT * FROM  Cm_Article where id=?",id);
	}

	public ExtUser getUserNameByUserId(String id){
		return ExtUser.dao.findFirst("SELECT * FROM  ext_user where id=?",id);
	}
	
	public String getId(){
		return this.getStr("id");
	}
}
